package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.entity.Result;
import com.itheima.service.LinkService;
import com.itheima.vo.LinkVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 友情链接接口
 */
@Controller
@RequestMapping("/link")
public class LinkController {

    @Autowired
    private LinkService linkService;

    /**
     * 增加友谊链
     * @param linkVo
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody LinkVo linkVo){
        linkService.add(linkVo);
        return new Result("success","添加成功");
    }

    /**
     * 获取友链列表
     * @param linkVo
     * @return
     */
    @PostMapping("/getList")
    @ResponseBody
    public Result list(@RequestBody(required = false) LinkVo linkVo){
       PageInfo pageInfo= linkService.list(linkVo);
       return new Result("success",pageInfo);
    }

    /**
     * 删除友链
     * @param linkVo
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Result delete(@RequestBody LinkVo linkVo){
        try {
            linkService.delete(linkVo);
            return new Result("success","删除成功");
        } catch (Exception e) {
            return new Result("fail","删除失败");
        }
    }

    /**
     * 修改友链
     * @param linkVo
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public Result edit(@RequestBody LinkVo linkVo){
        try {
            linkService.edit(linkVo);
            return new Result("success","修改成功");
        } catch (Exception e) {
            return new Result("fail","修改失败");
        }
    }

    @PostMapping("/stick")
    @ResponseBody
    public Result stick(@RequestBody LinkVo linkVo){
        try {
            linkService.stick(linkVo);
            return new Result("success","置顶成功");
        } catch (Exception e) {
            return new Result("fail","置顶失败");
        }
    }
}
